import java.util.List;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * user:likai
 * Date:2022-10-18
 * Time:16:48
 */
public class SignalList {
    static class Listnode{
        int val;
        Listnode next;
        public Listnode(int val) {
            this.val = val;
        }
    }
    //循环打印
    //递归
    public static void display(Listnode head){
        if(head==null){
            return;
        }
        display(head.next);
        System.out.println(head.val+" ");
    }
    //循环打印
    public static void dispalyCircle(Listnode head){
        Stack<Integer> stack=new Stack<>();
        while(head!=null){
            stack.push(head.val);
            head=head.next;
        }
        while(!stack.empty()){
            System.out.println(stack.pop());
        }
    }
    //逆波兰表达式
    public static int evalRPN(String[] tokens) {
        Stack<Integer> stack=new Stack<>();
        for (String x:tokens) {
            if(x.equals("*")||x.equals("/")||x.equals("+")||x.equals("-")){
                int num2=stack.pop();
                int num1=stack.pop();
                switch (x){
                    case "*":
                        stack.push(num1*num2);
                        break;

                    case "/":
                        stack.push(num1/num2);
                        break;

                    case "+":
                        stack.push(num1+num2);
                        break;
                    case "-":
                        stack.push(num1-num2);
                        break;
                }
            }else{
                stack.push(Integer.parseInt(x));
            }
        }
        return stack.pop();
    }

    public static void main(String[] args) {
        String []s={"2","1","+","3","*"};
        int ret=evalRPN(s);
        System.out.println(ret);
    }
    public static void main1(String[] args) {
        Listnode listnode1=new Listnode(1);
        Listnode listnode2=new Listnode(2);
        Listnode listnode3=new Listnode(3);
        listnode2.next=listnode3;
        listnode1.next=listnode2;
        dispalyCircle(listnode1);
    }
}
